package zhang.test.netfitsfabric;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.answers.AddToCartEvent;
import com.crashlytics.android.answers.Answers;
import com.crashlytics.android.answers.ContentViewEvent;
import com.crashlytics.android.answers.CustomEvent;
import com.crashlytics.android.answers.InviteEvent;
import com.crashlytics.android.answers.LevelEndEvent;
import com.crashlytics.android.answers.LevelStartEvent;
import com.crashlytics.android.answers.LoginEvent;
import com.crashlytics.android.answers.PurchaseEvent;
import com.crashlytics.android.answers.RatingEvent;
import com.crashlytics.android.answers.SearchEvent;
import com.crashlytics.android.answers.ShareEvent;
import com.crashlytics.android.answers.SignUpEvent;
import com.crashlytics.android.answers.StartCheckoutEvent;

import java.math.BigDecimal;
import java.util.Currency;

import io.fabric.sdk.android.Fabric;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 第五步，初始化(kit)fabric和Answer,注意一定要在初始化之前
        Fabric.with(this, new Answers(), new Crashlytics());
        loginUser();

        setContentView(R.layout.activity_main);
        //========================================================
        initView();
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
    }

    private void initView() {
        Button mSousuo = (Button) findViewById(R.id.sousuo);
        mSousuo.setOnClickListener(this);
        Button mContextView = (Button) findViewById(R.id.context_view);
        mContextView.setOnClickListener(this);
        Button mShare = (Button) findViewById(R.id.share);
        mShare.setOnClickListener(this);
        Button racontent = (Button) findViewById(R.id.Rated_content);
        racontent.setOnClickListener(this);
        Button signup = (Button) findViewById(R.id.Rated_sign_up);
        signup.setOnClickListener(this);
        Button login = (Button) findViewById(R.id.login_in);
        login.setOnClickListener(this);

        Button invite = (Button) findViewById(R.id.invite);
        invite.setOnClickListener(this);
        Button levelsatrt = (Button) findViewById(R.id.level_start);
        levelsatrt.setOnClickListener(this);
        Button levelend = (Button) findViewById(R.id.level_end);
        levelend.setOnClickListener(this);

        Button mEvent = (Button) findViewById(R.id.custom_event);
        mEvent.setOnClickListener(this);
        Button mArrributes = (Button) findViewById(R.id.custom_attributes);
        mArrributes.setOnClickListener(this);
    }

    // 第六步，实验===============================================================
    // TODO: Move this method and use your own event name to track your key metrics
    public void onKeyMetric(View view) {
        // TODO: Use your own string attributes to track common values over time
        // TODO: Use your own number attributes to track median value over time
        // 这是自定义事件
        Answers.getInstance().logCustom(new CustomEvent("CONNECTION ERROR")
                // 左边是定义的类型，右边是信息
                .putCustomAttribute("Category", "Comedy")
                .putCustomAttribute("Length", 350));
    }
    //==============================================================================

    public void forceCrash(View view) {
        throw new RuntimeException("This is a crash");
    }

    private void loginUser() {
        // 上传信息
        Crashlytics.setUserIdentifier("12345");
        Crashlytics.setUserEmail("user@fabric.io");
        Crashlytics.setUserName("Test User");
    }


    public void add(View view) {
        // 添加到购物车
        Answers.getInstance().logAddToCart(new AddToCartEvent()
                .putItemPrice(BigDecimal.valueOf(13.50))
                .putCurrency(Currency.getInstance("USD"))
                .putItemName("Answers Shirt")
                .putItemType("Apparel")
                .putItemId("sku-350"));

        Toast.makeText(this, "点击", Toast.LENGTH_SHORT).show();
    }

    public void pay(View view) {
        // 开始结账
        Answers.getInstance().logStartCheckout(new StartCheckoutEvent()
                // 货币价格
                .putTotalPrice(BigDecimal.valueOf(123.50))
                // 货币的样式
                .putCurrency(Currency.getInstance("USD"))
                // 物品的数量
                .putItemCount(3));

        Toast.makeText(this, "点击", Toast.LENGTH_SHORT).show();
    }

    public void purchase(View view) {
        // 采购
        Answers.getInstance().logPurchase(new PurchaseEvent()
                // 货币价格
                .putItemPrice(BigDecimal.valueOf(13.50))
                // 货币指定
                .putCurrency(Currency.getInstance("USD"))
                // 该项目的可读名称
                .putItemName("Answers Shirt")
                // 该项目下的类别
                .putItemType("Apparel")
                // 用于追踪项目的唯一标示符
                .putItemId("sku-350")
                .putSuccess(true));
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sousuo: // 搜索
                suosuo();
                break;

            case R.id.context_view: // 内容浏览
                contentView();
                break;

            case R.id.share: // 分享
                share();
                break;

            case R.id.Rated_content: // 评分内容
                ratedContent();
                break;

            case R.id.Rated_sign_up: // 注册
                signup();
                break;

            case R.id.login_in: // 登录
                login();
                break;

            case R.id.invite: // 邀请
                invite();
                break;

            case R.id.level_start: //启动级
                levelStart();
                break;

            case R.id.level_end: // 末级
                levelEnd();
                break;

            case R.id.custom_attributes: // 自定义属性
                customAttributes();
                break;

            case R.id.custom_event: // 自定义事件
                customEvent();
                break;
        }
    }

    private void customEvent() {
        Answers.getInstance().logCustom(new CustomEvent("Video Played"));

        Toast.makeText(this, "event", Toast.LENGTH_SHORT).show();
    }

    private void customAttributes() {
        // 自定义事件的属性
        Answers.getInstance().logCustom(new CustomEvent("Tweet Viewed")
                .putCustomAttribute("Media Type", "Image")
                .putCustomAttribute("Length", 120));

        Toast.makeText(this, "arrtibuts", Toast.LENGTH_SHORT).show();
    }

    private void levelEnd() {
        // 启动级结束，可以用户是否能达到这个水平
        Answers.getInstance().logLevelEnd(new LevelEndEvent()
                // 描述的关键水平
                .putLevelName("Level 2A")
                // 这个级别的比分
                .putScore(350)
                // 完成了水平或失败的尝试
                .putSuccess(true));

        Toast.makeText(this, "levelEnd", Toast.LENGTH_SHORT).show();
    }

    private void levelStart() {
        // 启动级开始，可以看到用户在启动水平的实时
        Answers.getInstance().logLevelStart(new LevelStartEvent()
                // 说明水平字符串键
                .putLevelName("Level 2A"));

        Toast.makeText(this, "levelStart", Toast.LENGTH_SHORT).show();
    }

    private void invite() {
        // 邀请
        Answers.getInstance().logInvite(new InviteEvent()
                // 用于邀请的方法 twitter
                .putMethod("Twitter"));

        Toast.makeText(this, "invite", Toast.LENGTH_SHORT).show();
    }

    private void login() {
        Answers.getInstance().logLogin(new LoginEvent()
                .putMethod("Digits")
                .putSuccess(true));

        Toast.makeText(this, "login", Toast.LENGTH_SHORT).show();
    }

    private void signup() {
        // 注册
        Answers.getInstance().logSignUp(new SignUpEvent()
                // 注册使用的方法
                .putMethod("Digits")
                // 注册成功与失败
                .putSuccess(true));

        Toast.makeText(this, "signup", Toast.LENGTH_SHORT).show();
    }

    private void ratedContent() {
        // 评分内容
        Answers.getInstance().logRating(new RatingEvent()
                // 内容的整数评级
                .putRating(4)
                // 内容的可读名称
                .putContentName("Answers setup process super easy!")
                // 您的项目属于下的类别
                .putContentType("Technical documentation")
                // 用于追踪项目的唯一标识符
                .putContentId("article-350"));

        Toast.makeText(this, "ratedcontent", Toast.LENGTH_SHORT).show();
    }

    private void share() {
        // 分享
        Answers.getInstance().logShare(new ShareEvent()
                // 用这个方法来共享内容，例如twitter
                .putMethod("Twitter")
                // 分享的内容
                .putContentName("Answers named #2 in Mobile Analytics")
                // 分享内容的类型
                .putContentType("tweet")
                // 唯一键，标识内容
                .putContentId("601072000245858305"));

        Toast.makeText(this, "share", Toast.LENGTH_SHORT).show();
    }

    private void contentView() {
        // 内容浏览
        Answers.getInstance().logContentView(new ContentViewEvent()
                // 内容的可读名称
                .putContentName("Answers setup process super easy!")
                // 项目的类别
                .putContentType("Technical documentation")
                // 追踪项目的唯一标识符
                .putContentId("article-350"));

        Toast.makeText(this, "contentview", Toast.LENGTH_SHORT).show();
    }

    private void suosuo() {
        // 搜索
        Answers.getInstance().logSearch(new SearchEvent()
                // 什么是用户搜索
                .putQuery("mobile analytics"));

        Toast.makeText(this, "sousuo", Toast.LENGTH_SHORT).show();
    }
}
